/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;

import dp1.titandevelop.titano.bean.EmpleadoSeleccionado;
import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.helper.HelperProcess;
import dp1.titandevelop.titano.persistent.Asignacion;
import dp1.titandevelop.titano.service.AsignacionService;
import dp1.titandevelop.titano.service.EmpleadoXProcesoTurnoService;
import dp1.titandevelop.titano.service.ProductoService;
import dp1.titandevelop.titano.view.MensajeError;
import java.awt.Cursor;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Yulian
 */
public class ModulosT_PRO_GuardarAsignacion extends javax.swing.JInternalFrame {

    private ArrayList<EmpleadoSeleccionado> listaAsignacion;
    private int indice;
    
    public ModulosT_PRO_GuardarAsignacion() {
        initComponents();
    }
    
     public ModulosT_PRO_GuardarAsignacion(ArrayList<EmpleadoSeleccionado> lista,int idAsignacion) {
        initComponents();
        AsignacionService serv = new AsignacionService();
        EmpleadoSeleccionado e= new EmpleadoSeleccionado();
        indice=idAsignacion;
        this.listaAsignacion=lista;
        this.txtCodAsig.setText(""+idAsignacion);
        
        this.txtTotal.setText(""+e.ObtenerProduccionTotal(lista));
        this.txtMerma.setText(""+""+e.ObtenerRoturaTotal(lista));
       
        this.tblResultados.setModel(ModeloTabla);
        TableRowSorter<TableModel> sorter=new TableRowSorter<TableModel>((TableModel)this.tblResultados.getModel());
        this.tblResultados.setRowSorter(sorter);
    }

         private AbstractTableModel ModeloTabla = new AbstractTableModel() {
        //Atributos de los datos de la tabla
        private String[] titles = {"Turno","Galleta","Empleado","Proceso","Produce","Desperdicia"};
        
        public int getRowCount() {
            return listaAsignacion.size();//tipos.size(); // cuantos registros (filas)
        }

        public int getColumnCount() {
            return 6; // cuantas columnas
        }

        @Override
        public String getColumnName(int col) {
            return titles[col];
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Object resultado = null;            
            EmpleadoXProcesoTurnoService serv = new EmpleadoXProcesoTurnoService();
            ProductoService servProducto =new ProductoService();
            int idEmp = listaAsignacion.get(rowIndex).getEmpleadoProceso().getToEmpleado().getIdempleado();
            int idTur = listaAsignacion.get(rowIndex).getEmpleadoProceso().getToTurno().getIdturno();
            int idProc = listaAsignacion.get(rowIndex).getEmpleadoProceso().getToProceso().getIdproceso();
        //   Integer ass= listaAsignacion.get(rowIndex);
            switch (columnIndex) {
                case 0:resultado =listaAsignacion.get(rowIndex).getEmpleadoProceso().getToTurno().getDescripcion();break;
                case 1: resultado =servProducto.buscarPorId(listaAsignacion.get(rowIndex).getGalleta()).getDescripcion();break;
                case 2:resultado = listaAsignacion.get(rowIndex).getEmpleadoProceso().getToEmpleado().getNombres()+ " "+listaAsignacion.get(rowIndex).getEmpleadoProceso().getToEmpleado().getApellidop();break;
                case 3:resultado = listaAsignacion.get(rowIndex).getEmpleadoProceso().getToProceso().getDescripcion();;break;
                case 4:resultado = serv.buscarUnEmpleadoXProceso(idEmp,idTur,idProc).getProductividad();break;
                case 5:resultado = serv.buscarUnEmpleadoXProceso(idEmp,idTur,idProc).getRotura();
            }
            return resultado;
        }
    };
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCodAsig = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtDescripcionAsig = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultados = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtMerma = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setResizable(true);
        setTitle("Guardar Asignación");

        jLabel1.setText("Código de Asignación:");

        txtCodAsig.setEditable(false);

        jLabel2.setText("Descripción:");

        txtDescripcionAsig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDescripcionAsigActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalle de Asignación"));

        tblResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Empleado", "Turno", "Proceso", "Produce", "Desperdicia"
            }
        ));
        jScrollPane1.setViewportView(tblResultados);

        jLabel3.setText("Total Producido:");

        txtTotal.setEditable(false);

        jLabel4.setText("Total Desperdiciado:");

        txtMerma.setEditable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 687, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(txtMerma, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtMerma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10))
        );

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/grabar.png"))); // NOI18N
        btnGuardar.setToolTipText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtCodAsig, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDescripcionAsig, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, 0)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodAsig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(txtDescripcionAsig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnGuardar)
                    .addComponent(btnCancelar))
                .addGap(10, 10, 10))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        try{
        Mensajes m=new Mensajes();    
        HelperProcess.guardarSolucion(listaAsignacion, indice,txtDescripcionAsig.getText());
        JOptionPane.showInternalMessageDialog(this.getRootPane(),m.getAgregado(),"Mensaje",JOptionPane.INFORMATION_MESSAGE);
        
        ModulosT_PRO_AsiRec  admAsig = new ModulosT_PRO_AsiRec();
        this.getParent().add(admAsig);
        
        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = admAsig.getSize();
        admAsig.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
            (desktopSize.height - jInternalFrameSize.height) / 4);
        admAsig.show();
        
        this.dispose();
        this.setCursor(Cursor.getDefaultCursor());
        }catch(Exception e) {
            //e.printStackTrace();
            MensajeError dialog = new MensajeError(new javax.swing.JFrame(), true);
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
            }
        
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
       ModulosT_PRO_AsiRec  admAsig = new ModulosT_PRO_AsiRec();
        this.getParent().add(admAsig);
        
        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = admAsig.getSize();
        admAsig.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
            (desktopSize.height - jInternalFrameSize.height) / 4);
        admAsig.show();
        
        this.dispose();
        
        
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtDescripcionAsigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDescripcionAsigActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDescripcionAsigActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblResultados;
    private javax.swing.JTextField txtCodAsig;
    private javax.swing.JTextField txtDescripcionAsig;
    private javax.swing.JTextField txtMerma;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables
}
